""" -*- coding: utf-8 -*-"""
from sqlalchemy import Enum

from utils.exts import db

"""部门表"""


class department(db.Model):
    __tablename__ = 'department'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64), unique=True)
    number = db.Column(db.String(16))
    manager = db.Column(db.String(64))
    mobile = db.Column(db.String(11))
    address = db.Column(db.String(64))
    description = db.Column(db.String(64))
    status = db.Column(Enum('正常', '禁用'), default='正常')

    users = db.relationship('User', backref='department')
    projects = db.relationship('Project', backref='department')

    def __repr__(self):
        return '<Department %r>' % self.name

    def to_json(self):
        json_department = {
            'id': self.id,
            'name': self.name,
            'description': self.description
        }
        return json_department


"""角色表"""


class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64), unique=True)
    description = db.Column(db.String(64))

    users = db.relationship('User', backref='role')

    def __repr__(self):
        return '<Role %r>' % self.name

    def to_json(self):
        json_role = {
            'id': self.id,
            'name': self.name,
            'description': self.description
        }
        return json_role


"""用户表"""


class Usermodel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(64))
    email = db.Column(db.String(64), unique=True)
    mobile = db.Column(db.String(11), unique=True)
    password = db.Column(db.String(64))
    is_active = db.Column(db.Boolean, default=True)

    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    department_id = db.Column(db.Integer, db.ForeignKey('department.id'))

    def __repr__(self):
        return '<Usermodel %r>' % self.username

    def to_json(self):
        json_user = {
            'id': self.id,
            'username': self.username,
            'email': self.email,
            'mobile': self.mobile,
            'password': self.password,
            'is_active': self.is_active,
            'role_id': self.role_id,
            'department_id': self.department_id
        }
        return json_user


"""项目表"""


class ProjectModel(db.Model):
    __tablename__ = 'project'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    project_code = db.Column(db.String(64), unique=True)
    project_name = db.Column(db.String(64))
    description = db.Column(db.String(64))
    create_time = db.Column(db.DateTime)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    department_id = db.Column(db.Integer, db.ForeignKey('department.id'))

    def __repr__(self):
        return '<ProjectModel %r>' % self.project_name

    def to_json(self):
        json_project = {
            'id': self.id,
            'project_code': self.project_code,
            'project_name': self.project_name,
            'description': self.description,
            'create_time': self.create_time,
            'user_id': self.user_id,
            'department_id': self.department_id
        }
        return json_project


"""设计阶段表"""


class DesignStageModel(db.Model):
    __tablename__ = 'design_stage'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64), unique=True)
    description = db.Column(db.String(64))
    filetypes = db.relationship('FileTypeModel', backref='design_stage')

    def __repr__(self):
        return '<DesignStageModel %r>' % self.name

    def to_json(self):
        json_file_type = {
            'id': self.id,
            'name': self.name,
            'description': self.description
        }
        return json_file_type


"""文件类型表"""


class FileTypeModel(db.Model):
    __tablename__ = 'file_type'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64), unique=True)
    description = db.Column(db.String(64))
    design_stage_id = db.Column(db.Integer, db.ForeignKey('design_stage.id'))

    def __repr__(self):
        return '<FileTypeModel %r>' % self.name

    def to_json(self):
        json_file_type = {
            'id': self.id,
            'name': self.name,
            'description': self.description
        }


"""存档文件表"""


class ArchiveFileModel(db.Model):
    __tablename__ = 'archive_file'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(64), unique=True)
    create_time = db.Column(db.DateTime)
    file_path = db.Column(db.String(64))
    description = db.Column(db.String(64))

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    project_id = db.Column(db.Integer, db.ForeignKey('project.id'))
    file_type_id = db.Column(db.Integer, db.ForeignKey('file_type.id'))
    design_stage_id = db.Column(db.Integer, db.ForeignKey('design_stage.id'))

    def __repr__(self):
        return '<ArchiveFileModel %r>' % self.name

    def to_json(self):
        json_file = {
            'id': self.id,
            'name': self.name,
            'create_time': self.create_time,
            'file_path': self.file_path,
            'description': self.description,
            'user_id': self.user_id,
            'project_id': self.project_id,
            'file_type_id': self.file_type_id,
            'design_stage_id': self.design_stage_id
        }
